package com.fpc.cn.springbootutil.base;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Component
public class BaseUtilService {
    private static final Logger log = LoggerFactory.getLogger(BaseUtilService.class);
    @Autowired(
            required = false
    )
    protected HttpServletRequest request;
/*    @Resource
    private RedisTemplate<String, Object> redisTemplate;*/

    public BaseUtilService() {
    }

    public BaseResponse setResultSuccess() {
        return this.setResult(GetSetCode.HTTP_RES_CODE_200, "success", (Object)null);
    }

    public BaseResponse setResultSuccess(String msg) {
        return this.setResult(GetSetCode.HTTP_RES_CODE_200, msg, (Object)null);
    }

    public BaseResponse setResultSuccess(Object data) {
        return this.setResult(GetSetCode.HTTP_RES_CODE_200, "success", data);
    }

    public BaseResponse setResultError(String msg) {
        return this.setResult(GetSetCode.HTTP_RES_CODE_500, msg, (Object)null);
    }

    public BaseResponse setResult(Integer code, String msg, Object data) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(code);
        baseResponse.setMsg(msg);
        if (data != null) {
            baseResponse.setData(data);
        }

        return baseResponse;
    }

    /*public String getUserId() {
        LoginInfo loginUserInfo = this.getLoginUserInfo();
        return loginUserInfo == null ? null : loginUserInfo.getUserId();
    }

    public LoginInfo getLoginUserInfo() {
        String loginInfoString = this.request.getHeader("baseLoginInfo");
        if (!StringUtils.isBlank(loginInfoString) && !"null".equals(loginInfoString.toLowerCase())) {
            LoginInfo loginInfo = (LoginInfo)JSONObject.parseObject(loginInfoString, LoginInfo.class);
            if (StringUtils.isNotBlank(loginInfo.getUserName())) {
                try {
                    String decode = URLDecoder.decode(loginInfo.getUserName(), StandardCharsets.UTF_8.name());
                    loginInfo.setUserName(decode);
                } catch (UnsupportedEncodingException var4) {
                    throw new BusinessException("转码失败");
                }
            }

            return loginInfo;
        } else {
            throw new BusinessException(10403, "用户未登录");
        }
    }*/

    public void setSessionInfo(String key, String value) {
        HttpSession session = this.request.getSession();
        session.setAttribute(key, value);
    }

    public String getSessionInfo(String key) {
        HttpSession session = this.request.getSession();
        return (String)session.getAttribute(key);
    }

    public void removeSessionInfo(String key) {
        HttpSession session = this.request.getSession();
        session.removeAttribute(key);
    }

    public void setSessionInfoObject(String key, Object value) {
        HttpSession session = this.request.getSession();
        session.setAttribute(key, value);
    }

    public Object getSessionInfoObject(String key) {
        HttpSession session = this.request.getSession();
        return session.getAttribute(key);
    }
}